<template>
  <div>
    <!-- 定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>资讯信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>留言信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 定义卡片 -->
    <el-card class="box-card">

      <!-- 定义第一行数据 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryMessageInfo.query" clearable @clear="getMessageList">
            <el-button slot="append" icon="el-icon-search" @click="getMessageList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">添加评论信息</el-button>
        </el-col>
      </el-row>

      <!-- 获取评论留言列表数据 -->
      <el-table :data="messageList" style="width: 100%" stripe border>
        <el-table-column type="index" label="序号" width="50px">
        </el-table-column>
        <el-table-column prop="fromName" label="评论者" width="70px">
        </el-table-column>
        <el-table-column prop="comment" label="内容" width="300px">
        </el-table-column>
        <el-table-column prop="toName" label="被评论者" width="300px">
        </el-table-column>
        <el-table-column prop="updated" :formatter="formatDate" label="评论时间" width="300px">
        </el-table-column>
        <el-table-column prop="likeNum" label="点赞数" width="300px">
        </el-table-column>
        <!-- 使用过滤器修改时间 在main.js中定义过滤器 -->
        <!-- <el-table-column prop="price" label="价格(元)" width="80px">
          <template slot-scope="scope">
            {{scope.row.price | priceFormat}}
          </template>
        </el-table-column>
        <el-table-column prop="num" label="数量" width="80px">
        </el-table-column>
        <el-table-column prop="status" label="状态" width="80px">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"
              @change="updateStatus(scope.row)"></el-switch>
          </template>
        </el-table-column> -->
        <!-- <el-table-column prop="updated" label="更新时间" width="180px" :formatter="formatDate">
         </el-table-column> -->

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="updateMessageBtn(scope.row)">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteMessageBtn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>


      <!-- 定义分页插件-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="queryMessageInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryMessageInfo.pageSize"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>

    </el-card>

    <!-- 定义评论添加的对话框 -->
    <el-dialog title="添加留言信息" :visible.sync="addDialogVisible" width="60%" @close="closeDialog">
      <!-- 准备修改的表单-->
      <el-form :model="addMessage" :rules="rules" ref="saveMessageFrom" label-width="100px" class="demo-ruleForm">
        <el-form-item label="评论者" prop="fromName" >
          <el-input v-model="addMessage.fromName"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="comment">
          <el-input v-model="addMessage.comment"></el-input>
        </el-form-item>
        <el-form-item label="被评论者" prop="toName">
          <el-input v-model="addMessage.toName"></el-input>
        </el-form-item>
        <el-form-item label="评论时间" prop="updated">
          <el-input v-model="addMessage.updated" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="点赞数" prop="likeNum">
          <el-input v-model="addMessage.likeNum"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveMessage">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 定义评论修改的对话框 -->
    <el-dialog title="留言信息修改" :visible.sync="updateDialogVisible" width="60%">
      <!-- 准备修改的表单-->
      <el-form :model="updateMessage" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="评论者" prop="fromName" >
          <el-input v-model="updateMessage.fromName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="comment">
          <el-input v-model="updateMessage.comment"></el-input>
        </el-form-item>
        <el-form-item label="被评论者" prop="toName">
          <el-input v-model="updateMessage.toName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="评论时间" prop="updated">
          <el-input v-model="updateMessage.updated" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="点赞数" prop="likeNum">
          <el-input v-model="updateMessage.likeNum"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateMessageById">确 定</el-button>
      </span>
    </el-dialog>



  </div>
</template>

<script>
export default {
  data() {
    return {
      //定义评论列表信息
      messageList: [],
      //1.定义商品列表信息
      // itemList: [],
      //2.定义分页对象
      queryMessageInfo: {
        query: '', //定义查询参数
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      addDialogVisible: false,
      addMessage: {},
      updateDialogVisible: false,
      updateMessage: {},
      //准备一个校验规则
      rules: {
        fromName: [
          { required: true, message: '请输入评论者姓名', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        comment: [
          { required: true, message: '请输入评论信息', trigger: 'blur' },
          { min: 5, max: 50, message: '长度在 5 到 50 个字符', trigger: 'blur' }
        ],
        toName: [
          { required: true, message: '请输入被评论者姓名', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        likeNum: [
          { required: true, message: '请输入喜欢人数', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  //vue生命周期
  created() {
    //1.获取评论列表数据
    this.getMessageList()
  },
  methods: {
    //实现评论信息分页查询
    async getMessageList() {
      const {data: result} =
        await this.$http.get("/message/getMessageList", {
          params: this.queryMessageInfo
        })
      if (result.status !== 200) return this.$message.error("评论列表查询失败")
      console.log(result.data.rows)
      this.messageList = result.data.rows
      this.total = result.data.total
    },
    //通过JS格式化时间
    formatDate(row, column, cellValue, index) {
      let date = new Date(cellValue)
      let year = date.getFullYear()
      let month = (date.getMonth() + 1 + '').padStart(2, '0')
      let day = (date.getDate() + '').padStart(2, '0')
      let HH = (date.getHours() + '').padStart(2, '0')
      let MM = (date.getMinutes() + '').padStart(2, '0')
      let SS = (date.getSeconds() + '').padStart(2, '0')
      return year + '-' + month + '-' + day + ' ' + HH + ":" + MM + ":" + SS
    },
    //条数变化时 调用
    handleSizeChange(size) {
      this.queryMessageInfo.pageSize = size
      this.getMessageList()
    },
    //页码值变化时 调用
    handleCurrentChange(current) {
      this.queryMessageInfo.pageNum = current
      this.getMessageList()
    },
    async deleteMessageBtn(message) {
      //消息确认框
      this.$confirm('此操作将永久删除该评论, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        //根据id删除数据
        const {data: result} = await this.$http.delete("/message/deleteMessageById", {
          params: {
            id: message.id
          }
        })
        if (result.status !== 200) return this.$message.error("评论删除失败")
        this.$message.success("评论删除成功")
        //重新获取评论列表信息
        this.getMessageList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    closeDialog(){
      //重置表格数据
      this.$refs.saveMessageFrom.resetFields()
    },
    //校验用户数据
    saveMessage(){
      this.$refs.saveMessageFrom.validate(async valid => {
        /* 如果校验不通过, 则返回false  如果校验通过 返回true */
        //console.log(valid)
        //如果校验失败 则停止数据
        if(!valid) return
        //console.log(this.addUserModel)
        const {data: result} = await this.$http.post('/message/saveMessage',this.addMessage)
        if(result.status !== 200) return this.$message.error("用户新增失败")
        this.$message.success("用户新增成功")
        //关闭对话框
        this.addDialogVisible = false
        //重新获取评论列表
        this.getMessageList()
      })
    },
    updateMessageBtn(message){
      this.updateDialogVisible = true
      this.updateMessage = message

    },
    async updateMessageById() {
      //修改教师信息
      const {data: result} = await this.$http.put('/message/updateMessageById', this.updateMessage)
      if (result.status !== 200){ return this.$message.error("更新评论信息失败")}
      this.$message.success("更新评论信息成功")
      this.updateDialogVisible = false;
      this.getMessageList();
    }
  }
}
</script>
<!-- 防止组件样式冲突 -->
<style lang="less" scoped>

</style>
